<?php
require_once("Database.class.php");
require_once("Database.object.class.php");
require_once("Session.class.php");
require_once("User_type.class.php");

class User extends Database_object
{
   protected $email;
   protected $password;
   protected $name;
   protected $ID;
   protected $home_address;
   protected $phone;
   protected $user_type;  
   protected $gender;
   protected static $db_fields = Array("ID" , "name" , "email" , "home_address" , "password" , "phone" , "user_type" , "gender");
   protected static $table_name = "user";

   public function set_id($ID)
   {
     $this->ID = $ID;
   }

   public function get_id()
   {
     return $this->ID;
   }

  public function set_name($name)
  {
    $this->name = $name;
  }

  public function get_name()
  {
    return $this->name;
  }

  public function set_password($password)
  {
    $this->password = $password;
  }

  public function get_password()
  {
   return $this->password;
  }

  public function set_phone($phone)
  {
    $this->phone = $phone;
  }

  public function get_phone()
  {
   return $this->phone;
  }

  public function set_gender($gender)
  {
   $this->gender = $gender;
  }

  public function get_gender()
  {
    return $this->gender;
  }

  public function set_home_address($home_address)
  {
   $this->home_address = $home_address; 
  }

  public function get_home_address()
  {
    return $this->home_address;
  }

  public function set_email($email)
  {
   $this->email = $email;
  }

  public function get_email()
  {
    return $this->email;
  }

  public function set_user_type($type)
  {
   $this->user_type = $type;
  }

  public function get_user_type()
  {
   return $this->user_type;
  }

   
 public static function authenicate($email="" , $password="") 
    { 
	    global $database;
	    $email = $database->escape_value($email);
	    $password = $database->escape_value($password);
	    $sql  = "SELECT * from user WHERE email='{$email}'";
	    $sql .= " AND password='{$password}'";
	    $sql .= " LIMIT 1";
	    $result = self::find_by_sql($sql);
      return !empty($result) ? array_shift($result): false;
    }

 public static function authenicate_ID($found_ID) 
    { 
	    global $database;
	    $email = $database->escape_value($found_ID);
	    $sql  = "SELECT * from user WHERE ID='{$found_ID}}'";
	    $result = self::find_by_sql($sql);
      return !empty($result) ? array_shift($result): false;
    }	
    
 public static function find_id($name)
    {
     global $database;
     $sql="SELECT ID FROM ". self::$table_name ." WHERE name='{$name}'";
     $result = $database->doQuery($sql);
     $row = mysqli_fetch_array($result);
     return $row['ID'];
    }

 public static function find_name($ID)
    {
     global $database;
     $sql="SELECT name FROM ". self::$table_name ." WHERE ID='{$ID}'";
     $result = $database->doQuery($sql);
     $row = mysqli_fetch_array($result);
     return $row['name'];
    }
	
   public static function find_user_type($ID)
    {
     global $database;
     $sql="SELECT user_type FROM ". self::$table_name ." WHERE ID='{$ID}'";
     $result = $database->doQuery($sql);
     $row = mysqli_fetch_array($result);
     return $row['user_type'];
    }
	

}
?>